package spike.utils;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 * Created by ylyang on 12/04/2017.
 */
public class CounterWheels<T> {
    HashMap<T, Integer> map;

    public CounterWheels() {
        map = new HashMap<>();
    }

    public void count(T e) {
        map.put(e, (1 + map.getOrDefault(e, 0)));
    }

    public Map<T, Integer> getMap() {
        return map;
    }

    public Stream<Map.Entry<T, Integer>> orderByCountDesc() {
        return map.entrySet().stream().sorted((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
    }

    public Stream<Map.Entry<T, Integer>> orderByCountAsc() {
        return map.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getValue));
    }
}
